home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1996 #15 / Monster Media Number 15 (Monster Media)(July 1996).ISO / prog_d / odbcall.zip / SORTDEMO.ZIP / UNIT1.PAS < prev   
Pascal/Delphi Source File  |  1995-11-05  |  3KB  |  110 lines

  1. unit Unit1;
  2.  
  3. interface
  4.  
  5. uses
  6.   SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  7.   Forms, Dialogs, StdCtrls, ExtCtrls, Grids, DataCtrl, TGQry, ODBCQry,
  8.   Buttons;
  9.  
  10. type
  11.   TForm1 = class(TForm)
  12.     ODBCCon1: TODBCCon;
  13.     ODBCQuery1: TODBCQuery;
  14.     DataGrid1: TDataGrid;
  15.     Panel1: TPanel;
  16.     ListBox1: TListBox;
  17.     Memo1: TMemo;
  18.     ClearSort: TBitBtn;
  19.     ReSort: TBitBtn;
  20.     Find: TButton;
  21.     Edit1: TEdit;
  22.     Label1: TLabel;
  23.     procedure ListBox1Click(Sender: TObject);
  24.     procedure FormCreate(Sender: TObject);
  25.     procedure ClearSortClick(Sender: TObject);
  26.     procedure ReSortClick(Sender: TObject);
  27.     procedure FindClick(Sender: TObject);
  28.   private
  29.     { Private declarations }
  30.     SortStr: String;
  31.   public
  32.     { Public declarations }
  33.   end;
  34.  
  35. var
  36.   Form1: TForm1;
  37.  
  38. implementation
  39.  
  40. {$R *.DFM}
  41.  
  42. procedure TForm1.ListBox1Click(Sender: TObject);
  43. var
  44.   CurField: String;
  45. begin
  46.   with ListBox1 do
  47.   begin
  48.     CurField := Items[ItemIndex];
  49.     if Copy(CurField, Length(CurField) - 3, 4) = ' des' then
  50.       CurField := Copy(CurField, 1, Length(CurField) - 4);
  51.  
  52.     if Pos(CurField, SortStr) > 0 then exit
  53.     else
  54.     begin
  55.       if Length(SortStr) > 0 then
  56.         SortStr := SortStr + ', ' + Items[ItemIndex]
  57.       else
  58.         SortStr := Items[ItemIndex];
  59.       ODBCQuery1.Sort(SortStr);
  60.       Caption := SortStr;
  61.     end;
  62.   end;
  63. end;
  64.  
  65. procedure TForm1.FormCreate(Sender: TObject);
  66. var
  67.   Index: Integer;
  68. begin
  69.   SortStr := '';
  70.   with ODBCQuery1 do
  71.   begin
  72.     Prepared := True;
  73.     FieldByName('CharField').DisplaySize := 20;
  74.     Active := True;
  75.     { fill listbox1 with a list of fields, use des for descending sorts }
  76.     for Index := 1 to FieldCount do
  77.     begin
  78.       ListBox1.Items.Add(Field[Index].FieldName);
  79.       ListBox1.Items.Add(Field[Index].FieldName + ' des');
  80.     end;
  81.     SortStr := 'DateField des, CharField';
  82.     Sort(SortStr); { sort on datefield descending and charfield ascending (default order)}
  83.     Caption := SortStr;
  84.   end;
  85. end;
  86.  
  87. procedure TForm1.ClearSortClick(Sender: TObject);
  88. begin
  89.   SortStr := '';
  90.   Caption := 'Un-sorted';
  91. end;
  92.  
  93. procedure TForm1.ReSortClick(Sender: TObject);
  94. begin
  95.   ODBCQuery1.ReSort;
  96. end;
  97.  
  98. procedure TForm1.FindClick(Sender: TObject);
  99. begin
  100.   { this only performs a search based on the first field in the sort order }
  101.   ODBCQuery1.Find([Edit1.Text]);
  102.  
  103.   { to search on more than one field use the following syntax:
  104.     Query.Find([value1, value2, ..., valuen]);
  105.     See TVarRec or Type safe open arrays in the Delphi help for more
  106.     info on array of const. }
  107. end;
  108.  
  109. end.
  110.